var app = getApp();
var api = app.api;
Page({
    data: {
        form: {
            name: ''
            , mobile: ''
        }
        , img: "/images/img-share-un.png"
        , agree: 0
        , show_modal: false
        , submit_ok: false
    }
    , onLoad: function (options) {
        getApp().page.onLoad(this, options);
    }
    , onReady: function () {
        getApp().page.onReady(this);
    }
    , onShow: function () {
        getApp().page.onShow(this);
        var self = this;
        var share_setting = getApp().core.getStorageSync(getApp().const.SHARE_SETTING);
        
        getApp().getConfig(function (config) {
            self.setData({
                share_name: '联创会员56折扣'
            });
        });

        self.setData({
            share_setting: share_setting
        });
    }
    , onHide: function () {
        getApp().page.onHide(this);
    }
    , onUnload: function () {
        getApp().page.onUnload(this)
    }
    , formSubmit: function (e) {
        var self = this;
        self.data.form = e.detail.value;
        
        if (self.data.form.name == undefined || self.data.form.name == '') {
            wx.showToast({
                title: "请填写姓名",
                icon: "none"
            });
            return false;
        }

        if (self.data.form.mobile == undefined || self.data.form.mobile == '') {
            wx.showToast({
                title: "请填写手机号码",
                icon: "none"
            });
            return false;
        }

        var check_mobile = /^\+?\d[\d -]{8,12}\d/;
        if (!check_mobile.test(self.data.form.mobile)) {
            getApp().core.showModal({
                title: '提示',
                content: '手机号格式不正确',
                showCancel: false
            });
            return false;
        }

        var data = e.detail.value;
        if (self.data.agree == 0) {
            wx.showToast({
                title: "请先阅读并确认申请协议",
                icon: "none"
            });
            return false;
        }

        getApp().core.showLoading({
            title: "正在提交",
            mask: true
        });

        getApp().request({
            url: getApp().api.share.apply_discount,
            method: 'POST',
            data: data,
            complete: function () {
                getApp().core.hideLoading();
            },
            success: function (res) {
                if (res.code == 0) {
                    self.setData({submit_ok: true});
                } else {
                    wx.showToast({
                        title: res.msg,
                        icon: "none"
                    });
                }
            }
        });
    }
    , agreement: function () {
        var self = this;
        self.setData({
            show_modal: true
        });
    }
    , agree: function () {
        var self = this;
        var agree = self.data.agree;
        if (agree == 0) {
            agree = 1;
            self.setData({
                img: "/images/img-share-agree.png",
                agree: agree
            });
        }
        else if (agree == 1) {
            agree = 0;
            self.setData({
                img: "/images/img-share-un.png",
                agree: agree
            });
        }
    }
    , close: function () {
        var self = this;
        self.setData({
            show_modal: false,
            img: "/images/img-share-agree.png",
            agree: 1
        });
    }
});
